package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.Spu;
import com.leyou.item.mapper.SpuMapper;
import com.leyou.item.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {
    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SpecParamService specParamService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Override
    public PageDTO<SpuDTO> getpage(Integer page, Integer rows, Long bid, Long cid, Long id, Boolean saleable) {

        Page<Spu> spuPage = new Page<>(page,rows);
        page(spuPage,new QueryWrapper<Spu>()
        .eq(id!=null,"id",id)
        .eq(bid!=null,"brand_id",bid)
        .eq(cid!=null,"cid3",cid)
        .eq(saleable!=null,"saleable",saleable));

        List<SpuDTO> spuDTOS = SpuDTO.convertEntityList(spuPage.getRecords());

        spuDTOS.forEach(spuDTO -> {
            //根据品牌id获取对应品牌对象,获取name
//            Long i =spuDTO.getBrandId();
//            Brand byId = brandService.getById(i);
//            String name = byId.getName();
//            spuDTO.setBrandName(name);
            spuDTO.setBrandName(this.brandService.getById(spuDTO.getBrandId()).getName());

            String names =this.categoryService
                    .queryCategoryByIds(spuDTO.getCategoryIds())
                    .stream()
                    .map(CategoryDTO::getName)
                    .collect(Collectors.joining("/"));
            spuDTO.setCategoryName(names);

        });

        return new PageDTO<>(spuPage.getTotal(),spuPage.getPages(),spuDTOS);
    }

    @Override
    public List<SpecParamDTO> querySpecsValues(Long id, Boolean searching) {
        //获取分类，根据分类查询其规格参数,同时传入searching
       List<SpecParamDTO> paramlist = specParamService.queryParams(
               null,getById(id).getCid3(),searching);
        //获取规格属性的键值对，key为规格参数的id，value为对应的规格值，nativeRead，json粉碎机，什么类型都可以处理
        Map<Long,Object> specMap = JsonUtils.nativeRead(
            spuDetailService.getById(id).getSpecification(),
                new TypeReference<Map<Long,Object>>(){});

        //循环给规格参数添加属性，value
        paramlist.forEach(specParamDTO -> {
            specParamDTO.setValue(specMap.get(specParamDTO.getId()));
        });
        return paramlist;
    }

    @Override
    public SpuDTO querySpuGetById(Long id) {

        return new SpuDTO(getById(id));
    }
}
